<?php 
namespace app\admin\controller;
use \think\Controller;
/**
* 前台用户有关设置
*/
class Member extends Base{
	public function administer(){
		$test=model("TkFrontUser");
		$model=$test->order("mid desc")->paginate('5',false);
		$num=count($model);
		if(!input("param.iffor") && !input("param.ifrec")){
				if($num!=0){	
				$page=$model->render();
				$result=[];
				foreach ($model as $k => $v) {
					$result[]=$v->toArray();
				}
				$uname=[];
				$sex=[];
				foreach ($result as $k => $v) {
					$mid[]=$v['mid'];
					$uname[]=$v['uname'];
					$sex[]=$v['sex'];
					$email[]=$v['email'];
					$type[]=$v['type'];
					$lasttime[]=$v['lastlogintime'];
					$lastip[]=$v['lastloginip'];
					$status[]=$v['status'];
				}
				//获取总条数和总页数
				$model=$test->count();
				$a=1;
				if(!isset($_GET['page'])){
					$_GET['page']=1;
				}
				$nowpage=$_GET['page'];
				$list=(($nowpage-1)*5)+1;
				$pagenum=ceil($num/5);
				$this->assign("list",$list);
				$this->assign("a",$a);
				$this->assign("num",$num);
				$this->assign("pagenum",$pagenum);
				$this->assign("page",$page);
				$this->assign("status",$status);
				$this->assign("mid",$mid);
				$this->assign("uname",$uname);
				$this->assign("sex",$sex);
				$this->assign("email",$email);
				$this->assign("type",$type);
				$this->assign("lasttime",$lasttime);
				$this->assign("lastip",$lastip);
				return $this->fetch();
		}else{
			$a=2;
			$this->assign("a",$a);
			return $this->fetch();
		}
	}else{
		if(input("param.iffor")){	
			$test=model("TkFrontUser");
			$result=$test->where(['ifforbidden'=>input("param.iffor")])->paginate("5",false);
			$num=count($result);
			$page=$result->render();
			if($num!=0){
				foreach($result as $k => $v) {
					$status[]=$v->toArray();
				}
				foreach ($status as $k => $v) {
					$mid[]=$v['mid'];
					$uname[]=$v['uname'];
					$sex[]=$v['sex'];
					$email[]=$v['email'];
					$type[]=$v['type'];
					$lasttime[]=$v['lastlogintime'];
					$lastip[]=$v['lastloginip'];
					$status[]=$v['status'];
				}
				$a=3;
				$num=$result->count();
				$pagenum=ceil($num/5);
				if(!isset($_GET['page'])){
					$_GET['page']=1;
				}
				$nowpage=$_GET['page'];
				$list=(($nowpage-1)*5)+1;
				$this->assign("a",$a);
				$this->assign("num",$num);
				$this->assign("list",$list);
				$this->assign("pagenum",$pagenum);
				$this->assign("page",$page);
				$this->assign("status",$status);
				$this->assign("mid",$mid);
				$this->assign("uname",$uname);
				$this->assign("sex",$sex);
				$this->assign("email",$email);
				$this->assign("type",$type);
				$this->assign("lasttime",$lasttime);
				$this->assign("lastip",$lastip);
				return $this->fetch();
			}else{
				$a=4;
				$this->assign("a",$a);
				return $this->fetch();
			}
		}
		if(input("param.ifrec")){
			$test=model("TkFrontUser");
			$result=$test->where(['ifrecommend'=>input("param.ifrec")])->paginate("5",false);
			$num=count($result);
			$page=$result->render();
			if($num!=0){
				foreach($result as $k => $v) {
					$status[]=$v->toArray();
				}
				foreach ($status as $k => $v) {
					$mid[]=$v['mid'];
					$uname[]=$v['uname'];
					$sex[]=$v['sex'];
					$email[]=$v['email'];
					$type[]=$v['type'];
					$lasttime[]=$v['lastlogintime'];
					$lastip[]=$v['lastloginip'];
					$status[]=$v['status'];
				}
				$a=5;
				$num=$result->count();
				$pagenum=ceil($num/5);
				if(!isset($_GET['page'])){
					$_GET['page']=1;
				}
				$nowpage=$_GET['page'];
				$list=(($nowpage-1)*5)+1;
				$this->assign("a",$a);
				$this->assign("list",$list);
				$this->assign("num",$num);
				$this->assign("pagenum",$pagenum);
				$this->assign("page",$page);
				$this->assign("status",$status);
				$this->assign("mid",$mid);
				$this->assign("uname",$uname);
				$this->assign("sex",$sex);
				$this->assign("email",$email);
				$this->assign("type",$type);
				$this->assign("lasttime",$lasttime);
				$this->assign("lastip",$lastip);
				return $this->fetch();
			}else{
				$a=6;
				$this->assign("a",$a);
				return $this->fetch();
			}
		}
	}
}
	//单个删除
	public function delete(){
		$mid=input("param.mid");
		$a=input("param.a");
		$model=model("TkFrontUser");
		$result=$model->where(['mid'=>$mid])->delete();
		if($result==1){
			if($a==1){
			$this->msg("删除成功","/admin/member/administer");
			}elseif($a==2){
				$this->msg("删除成功","/admin/member/administer/iffor/2");
			}elseif($a==3){
				$this->msg("删除成功","/admin/member/administer/ifrec/1");
			}
		}
	}
	//批量删除
	public function batch_delete(){
		if(request()->isAjax()){
		  $mid=input("post.id");
		  $model=model("TkFrontUser");
		  $result=$model->where(['mid'=>['in',$mid]])->delete();
		  if($result){
		  	return $this->success("删除成功");
		  }else{
		  	return $this->error("删除失败");
		  }
		}
	}
	//修改是否禁用状态 1是启用 0是禁用
	public function status(){
		$status=input("param.ifstatus");
		$mid=input("param.id");
		if($status==1){
			$model=model("TkFrontUser");
			$result=$model->save(['status'=>$status],['mid'=>$mid]);
			if($result==1){
				$this->msg("修改成功","/admin/member/administer");
			}
		}else{
			$model=model("TkFrontUser");
			$result=$model->save(['status'=>$status],['mid'=>$mid]);
			if($result==1){
				$this->msg("修改成功","/admin/member/administer");
			}
		}
	}
	public function edit(){
		$mid=input("param.mid");
		$result=model("TkFrontUser")->where(['mid'=>$mid])->select();
		$status=[];
		foreach ($result as $key => $value) {
			$status=$value->toArray();
		}	
		$iffor=$status['ifforbidden'];
		$ifrec=$status['ifrecommend'];
		$this->assign("mid",$mid);
		$this->assign("iffor",$iffor);
		$this->assign("ifrec",$ifrec);		
		return $this->fetch();
	}
	//eidt的ajax处理
	public function edit_ajax(){
		if(request()->isAjax()){
			$model=model("TkFrontUser");
			$data=(input("post.val"));
			$data=explode(",",$data);
			$mid=input("post.mid");
			//获取是否被禁言的值并修改
			$for=$data[0];
			$result=$model->save(['ifforbidden'=>$for],["mid"=>$mid]);
			$rec=$data[1];
			$res=$model->save(['ifrecommend'=>$rec],["mid"=>$mid]);
			if($result==1 || $res==1){
				return $this->success("修改成功");
			}else{
				return $this->error("选项没有发生改变,修改失败");
			}
	}	
}
	 public function msg($message,$url){
     echo "<script>alert('{$message}');location.href='{$url}'</script>";
    }
}
